package com.ede.roverhu.simplebackstage.Activities;


import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.ede.roverhu.simplebackstage.Fragments.PlaceholderFragment;
import com.ede.roverhu.simplebackstage.R;
import com.ede.roverhu.simplebackstage.Utils.IntentUtils;


public class MainActivity extends AppCompatActivity {
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;
    private TextView mTv_peopleManager;
    private TextView mTv_classManager;
    private TextView mTv_courseManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("智慧校园简易后台");
        setSupportActionBar(toolbar);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        mViewPager.addOnPageChangeListener(new pageChangeListener());

        mTv_peopleManager = (TextView) findViewById(R.id.tv_peopleManager);
        mTv_classManager = (TextView) findViewById(R.id.tv_classManager);
        mTv_courseManager = (TextView) findViewById(R.id.tv_courseManager);

        mTv_peopleManager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setTvDefault();
                setTvselected(mTv_peopleManager);
                mViewPager.setCurrentItem(0);
            }
        });
        mTv_classManager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setTvDefault();
                setTvselected(mTv_classManager);
                mViewPager.setCurrentItem(1);
            }
        });
        mTv_courseManager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setTvDefault();
                setTvselected(mTv_courseManager);
                mViewPager.setCurrentItem(2);
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_settings) {
            IntentUtils.startActivity(MainActivity.this, BlueToothActivity.class);
        }

        return super.onOptionsItemSelected(item);
    }


    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return PlaceholderFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return null;
        }

    }

    public class pageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            switch (position) {
                case 0:
                    setTvDefault();
                    setTvselected(mTv_peopleManager);
                    break;
                case 1:
                    setTvDefault();
                    setTvselected(mTv_classManager);
                    break;
                case 2:
                    setTvDefault();
                    setTvselected(mTv_courseManager);
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    //setdefault
    public void setTvDefault() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mTv_courseManager.setBackgroundColor(getColor(R.color.colorPrimary));
            mTv_courseManager.setTextColor(getColor(R.color.white));
            mTv_classManager.setBackgroundColor(getColor(R.color.colorPrimary));
            mTv_classManager.setTextColor(getColor(R.color.white));
            mTv_peopleManager.setBackgroundColor(getColor(R.color.colorPrimary));
            mTv_peopleManager.setTextColor(getColor(R.color.white));
        } else {
            mTv_courseManager.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            mTv_courseManager.setTextColor(getResources().getColor(R.color.white));
            mTv_classManager.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            mTv_classManager.setTextColor(getResources().getColor(R.color.white));
            mTv_peopleManager.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            mTv_peopleManager.setTextColor(getResources().getColor(R.color.white));
        }

    }

    //setselected
    public void setTvselected(TextView tv) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            tv.setBackgroundColor(getColor(R.color.white));
            tv.setTextColor(getColor(R.color.colorPrimary));
        } else {
            tv.setBackgroundColor(getResources().getColor(R.color.white));
            tv.setTextColor(getResources().getColor(R.color.colorPrimary));
        }

    }


}
